﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RobberSpawner : MonoBehaviour
{
    public Animator mAnimator;
    public GameObject Warning;
    private const float RandomOffset = 1f;
    private const float SpawnInterval = 0.03f;
    public int SpawnNum = 10;
    private int mCurrentSpawnNum;
    private bool Triggered = false;
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!Triggered && collision.gameObject.layer == PhysicsLayer.Player)
        {
            Player player = collision.gameObject.GetComponent<Player>();
            mAnimator.Play("Warning");
            StartCoroutine(Spawn());
            Triggered = true;
        }
    }
    IEnumerator Spawn()
    {
        GameObject.Find("RobberSound").GetComponent<AudioSource>().Play();
        mCurrentSpawnNum = 0;
        while (mCurrentSpawnNum < SpawnNum)
        {
            if (Random.Range(0f, 1f) < 0.5f)
            {
                GameObject text = GameObject.Instantiate(Resources.Load("Robber1")) as GameObject;
                text.transform.position = transform.position + Vector3.right * Random.Range(-RandomOffset, RandomOffset);
            }
            else
            {
                GameObject text = GameObject.Instantiate(Resources.Load("Robber2")) as GameObject;
                text.transform.position = transform.position + Vector3.right * Random.Range(-RandomOffset, RandomOffset);
            }
            mCurrentSpawnNum++;
            yield return new WaitForSeconds(SpawnInterval);
        }
        Warning.SetActive(false);
    }
}
